package com.bdqn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.entity.User;
import com.bdqn.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTester {
    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectById() {// 主键查询
        User user = userMapper.selectById(19L);
        System.out.println("usrName：" + user.getUsrName());
    }

    @Test
    public void testInsert() {
        userMapper.insert(new User("MyBatis-Plus", "123456", 8L, 1));
    }

    @Test
    public void testFindAll() {
        // 查询所有
        List<User> list = userMapper.selectList(null);
        for (User user : list) {
            System.out.println("usrName：" + user.getUsrName());
        }
    }

    @Test
    public void testDeleteById() {
        int list = userMapper.deleteById(27L);
        userMapper.deleteById(list);
        System.out.println("删除成功！");
    }

    @Test
    public void testFind() {
        //条件查询：根据用户名和密码查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_name", "MyBatis-Plus");
        wrapper.eq("usr_password", "123456");
        List<User> list = userMapper.selectList(wrapper);
        if (list != null) {
            for (User user : list) {
                System.out.println("usrId：" + user.getUsrId());
            }
        }
    }

    @Test
    public void testPagination() {// 分页查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_role_id", 2L);//查询条件：角色Id为2
        // 分页对象：查询获得User对象，查看第1页数据，每页显示2条记录
        Page<User> page = new Page<User>(1, 2);
        IPage<User> userPager = userMapper.selectPage(page, wrapper);
        System.out.println("总记录数 ---- --> " + userPager.getTotal());
        System.out.println("总页数 ------ --> " + userPager.getPages());
        System.out.println("当前页数 ----- -> " + userPager.getCurrent());
        System.out.println("每页记录数 -----> " + userPager.getSize());
        System.out.println("当前记录： -----> ");
        for (User user : userPager.getRecords()) {//获得查询记录
            System.out.println("usrId："+user.getUsrId());
            }
       }
}